package com.esp.webalbum.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import com.esp.webalbum.bean.ImageBean;
import com.esp.webalbum.ftp.WebFTPClient;
import com.esp.webalbum.utils.Const;
import com.esp.webalbum.utils.Log;
import com.esp.webalbum.utils.Utils;

public class SplashScreen extends Activity {
	
	private static String TAG = SplashScreen.class.getCanonicalName();
	
	public static ArrayList<ImageBean> arrayListImageBean = new ArrayList<ImageBean>();
	
	private ProgressDialog mProgressDialog ;
	private WebFTPClient appFTP;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splashscreen);
		Const.CONTEXT = SplashScreen.this;
		Utils.systemInitialization();
		
		
		mProgressDialog = new ProgressDialog(SplashScreen.this);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setMessage("Connecting to server.....");	
		mProgressDialog.setIndeterminate(false);
		mProgressDialog.setCancelable(false);
		
		
		AsyncTask<Void, Void, Integer> taskFTPHandler = new AsyncTask<Void, Void, Integer>() {
			
			private int intftpconn;
			
			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				mProgressDialog.show();
				
			}
			@Override
			protected Integer doInBackground(Void... params) {
				int Result = 0;
				appFTP = new WebFTPClient();
		        intftpconn = appFTP.getFTPConn();
		        
		        if(intftpconn == 200)
		        {
		        	Log.verbose(TAG,"AsyncTask :|==>> Ftp Login Successfully  ");
		        	arrayListImageBean = appFTP.imageFileNameList();
		        	
		        }else if(intftpconn == 201){
		        	Result = 201;
		        	Log.verbose(TAG,"AsyncTask :|==>> Ftp Login Error Please try Again  ");
		        	
		        }else if(intftpconn == 401){
		        	
		        	Result = 401;
		        	Log.verbose(TAG,"AsyncTask :|==>> Login Error Please try Again   ");
		        	
		        }
				
				return Result;
			}
			@Override
			protected void onPostExecute(Integer result) {
				super.onPostExecute(result);
				mProgressDialog.dismiss();
				
				if(result == 0){
					
					appFTP.releaseConnection();
					final Intent i = new Intent(SplashScreen.this, ImageDetailActivity.class);
			        i.putExtra(ImageDetailActivity.EXTRA_IMAGE, 0);
			        startActivity(i);
					
					
				}else if(result == 201){
					
		        	System.out.println("!!!  		Login Error Please try Again 	!!!!!");
		        	
		        }else if(result == 401){
		        	
		        	System.out.println("!!!  		Login Error Please try Again 	!!!!!");
		        }
			}
			
		};
		
		taskFTPHandler.execute();
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
}
